package com.nifostasky.mqcallback.producer;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 生产者(producer) -> 交换机(exchange) 的消息确认回调
 */
@Component
public class ConfirmProducer {
    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * confirm 确认模式
     */
    public void sentMsg(){
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        // "" 表示为默认交换机
        rabbitTemplate.convertAndSend("", "helloRabbitMQ","Hello RabbitMQ ~ ", correlationId);
    }

    public void sentMsg2(){
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        // 设置一个不存在的exchange 测试失败情况
        rabbitTemplate.convertAndSend("abc", "helloRabbitMQ","Hello RabbitMQ ~ ", correlationId);
    }

    /**
     * return 退回模式
     */
    public void sentMsg3(){
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        // 设置一个不存在的routingkey 测试失败情况
        rabbitTemplate.convertAndSend("", "helloRabbitMQ1", "Hello RabbitMQ ~ ", correlationId);
    }

    public void sentMsg4(){
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        // 设置一个存在的routingkey 测试成功的情况
        rabbitTemplate.convertAndSend("", "helloRabbitMQ", "Hello RabbitMQ ~ ", correlationId);
    }
}
